//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.Common.Configuration.Design;
using EntLibContrib.Common.Configuration.Design.Editors;
using EntLibContrib.Resource.Properties;

namespace EntLibContrib.Resource.Configuration.Design
{
	/// <summary>
	/// Represents a resource provider using an assembly.
	/// </summary>
	public sealed class AssemblyResourceProviderNode : ResourceProviderBaseNode
	{
		#region Fields
		private AssemblyResourceName assemblyResourceName;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the name of the resource.
		/// </summary>
		/// <value>The <see cref="AssemblyResourceName"/> data for this node.</value>
		[Required]
		[Editor(typeof(ResourceSelectorEditor), typeof(UITypeEditor))]
		[ResourceCategory("CategoryProvider")]
		[ResourceDescription("AssemblyResourceNameDescription")]
		public AssemblyResourceName ResourceName
		{
			get
			{
				return assemblyResourceName;
			}
			set
			{
				if (value != null)
				{
					assemblyResourceName = value;
				}
			}
		}

		/// <summary>
		/// Gets a <see cref="AssemblyResourceProviderData"/> configuration object using the node data.
		/// </summary>
		/// <value>
		/// A <see cref="AssemblyResourceProviderData"/> configuration object using the node data.
		/// </value>
		[ReadOnly(true)]
		public override ResourceProviderDataBase ResourceProviderData
		{
			get { return new AssemblyResourceProviderData(Name, assemblyResourceName.AssemblyName, assemblyResourceName.BaseName); }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="AssemblyResourceProviderNode"/> class.
		/// </summary>
		public AssemblyResourceProviderNode ()
			: this(new AssemblyResourceProviderData(InternalResources.DefaultAssemblyResourceProviderNodeName, string.Empty, string.Empty))
		{
		}

		/// <summary>
		/// Initialize a new instance of the<see cref="AssemblyResourceProviderNode"/> class with a <see cref="AssemblyResourceProviderData"/> configuration object.
		/// </summary>
		/// <param name="assemblyResourceProviderData">The assembly resource provider data.</param>
		public AssemblyResourceProviderNode (AssemblyResourceProviderData assemblyResourceProviderData)
		{
			if ( assemblyResourceProviderData == null )
				throw new ArgumentNullException("assemblyResourceProviderData");

			Rename(assemblyResourceProviderData.Name);
			this.assemblyResourceName = new AssemblyResourceName(assemblyResourceProviderData.ResourceAssemblyName, assemblyResourceProviderData.ResourceBaseName);
		}
		#endregion
	}
}